package com.uid.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.uid.model.UniqueIdSequence;
import com.uid.service.IUniqueIdSequenceService;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2018-02-19
 */
@Controller
@RequestMapping("/op")
public class UniqueIdSequenceController {

  @Resource
  private IUniqueIdSequenceService uniqueIdSequenceService;

  @RequestMapping("/query")
  @ResponseBody
  public Long queryUid(int type) throws Exception {
    return uniqueIdSequenceService.nextId(type);
  }

  @RequestMapping("/query/latest")
  @ResponseBody
  public UniqueIdSequence latestUid(int type) throws Exception {

    EntityWrapper<UniqueIdSequence> wrapper = new EntityWrapper<UniqueIdSequence>();
    wrapper
            .where("stub = {0}", type) // stub=?
            .orderBy(false, "id") // order by id desc
    ;
    //.where("stub = {0}",type).andNew("id > {0}",100).orderBy("id");
    UniqueIdSequence id = uniqueIdSequenceService.selectOne(wrapper);

    return id;
  }

}

